package chapter1.view;

import chapter2.vo.DeptVo;
import chapter2.vo.UserVo;
import chapter2.vo.RoleVo;
import chapter2.vo.Result;
import service.array.UserService;
import chapter2.dto.UserDto;
import service.IUserService;

public class UserView extends BaseView< UserVo, IUserService> {
    private static final String MENU = """
            ----------------
                 用户管理
            ----------------
            | 0 返回上级     |
            | 1 用户列表     |
            | 2 创建用户     |
            | 3 更新用户     |
            | 4 切换状态     |
            ----------------
            请输入选项：""";
    private static UserView singleton;



    private final DeptView deptView;
    private final RoleView roleView;


    private UserView() {
        deptView = DeptView.getInstance();
        roleView = RoleView.getInstance();
        service =UserService.getInstance();
    }

    public static UserView getInstance() {
        if (singleton == null) {
            singleton = new UserView();
        }
        return singleton;
    }


    public void show() {
        String menuChoice;
        do {
            System.out.println(MENU);
            menuChoice = MainView.input.nextLine();
            switch (menuChoice) {
                case "0" -> System.out.println("以返回上一级菜单!");
                case "1" -> list();
                case "2" -> create();
                case "3" -> update();
                case "4" -> toggle();
                default -> System.out.println("输入错误，请重新输入!");
            }
        } while (!"0".equals(menuChoice));
    }



    public void list() {
        Result result = service.list();
        cache = (UserVo[]) result.data;
        System.out.println("ID\t用户名\t姓名\t手机号\t邮箱\t部门名称\t角色名称\t状态");
        for (UserVo vo : cache) {
            System.out.printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", vo.getId(), vo.username, vo.name, vo.mobile, vo.email, vo.deptName, vo.roleName, vo.enabled ? "启用" : "禁用");
        }
    }

    public void create() {
        System.out.println("请输入用户名：");
        String username = MainView.input.nextLine();
        System.out.println("请输入姓名：");
        String name = MainView.input.nextLine();
        System.out.println("请输入手机号：");
        String mobile = MainView.input.nextLine();
        System.out.println("请输入邮箱：");
        String email = MainView.input.nextLine();

        System.out.println("请选择部门ID：");
        int deptId = 0;
        do {
            deptView.list();
            if (deptView.cache == null || deptView.cache.length == 0) {
                printError("部门列表为空，请先创建部门!");
                return;
            }
            DeptVo dept = deptView.getCacheById();
            if (dept != null) {
                deptId = dept.getId();
            }
        } while (deptId == 0);
        System.out.println("请选择角色ID：");

        int roleId = 0;
        do {
            roleView.list();
            if (roleView.cache == null || roleView.cache.length == 0) {
                printError("没有角色数据，请先创建角色!");
                return;
            }
            RoleVo role = roleView.getCacheById();
            if (role != null) {
                roleId = role.getId();
            }
        } while (roleId == 0);

        UserDto dto = new UserDto(username, name, mobile, email, deptId, roleId);
        Result result = service.create(dto);

        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public void update() {
        UserVo user = getCacheById();
        if (user == null) {
            printError("没有找到对应的数据，请重新输入!");
            return;
        }

        System.out.println("请输入用户名,直接按回车表示不修改");
        String username = MainView.input.nextLine();
        username = "".equals(username) ? user.username : username;
        System.out.println("请输入用户姓名,直接按回车表示不修改");
        String name = MainView.input.nextLine();
        name = "".equals(name) ? user.name : name;
        System.out.println("请输入手机号,直接按回车表示不修改");
        String mobile = MainView.input.nextLine();
        mobile = "".equals(mobile) ? user.mobile : mobile;
        System.out.println("请输入邮箱,直接按回车表示不修改");
        String email = MainView.input.nextLine();
        email = "".equals(email) ? user.email : email;
        System.out.println("请选择部门ID：");
        int deptId = 0;
        do {
            deptView.list();
            if (deptView.cache == null || deptView.cache.length == 0) {
                printError("部门列表为空，请先创建部门!");
                return;
            }
            DeptVo dept = deptView.getCacheById();
            if (dept != null) {
                deptId = dept.getId();
            }
        } while (deptId == 0);
        System.out.println("请选择角色ID：");

        int roleId = 0;
        do {
            roleView.list();
            if (roleView.cache == null || roleView.cache.length == 0) {
                printError("没有角色数据，请先创建角色!");
                return;
            }
            RoleVo role = roleView.getCacheById();
            if (role != null) {
                roleId = role.getId();
            }
        } while (roleId == 0);

        UserDto dto = new UserDto(username, name, mobile, email, deptId, roleId);
        Result result = service.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();

    }

    public void toggle() {
        UserVo user = getCacheById();
        if (user == null) {
            printError("没有找到对应的数据，请重新输入!");
            return;
        }
        if (user.enabled) {
            System.out.print("输入'Y'确认停用");
        } else {
            System.out.print("输入'Y'确认启用");
        }
        String confirm = MainView.input.nextLine();
        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("已取消操作!");
            return;
        }
        Result result = service.toggle(user.getId());
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }
}